<?php
include('image_upload.inc');
include('ShopDAO.inc');
if (isset($_REQUEST['scan']))
{
	$isSuccess;
	// 声明一个上传类
	$upfos = new image_upload('file', 'images');

	/**
	 * 建议使用随机生成的文件名，以避免上传重名的文件。
	 * 例如： $upfos->upload ( $upfos->newName());
	 */
	// 上传文件
	$isSuccess = $upfos->upload($upfos->newName());

	// 生成缩略图
	$isSuccess = $upfos->thumb('thumbnail-',200, 150);

	if ($isSuccess) {
		
		$uploadedimage = $upfos->filePath();
		$uploadedThumbImage = $upfos->thumbMap();
		
		$shopid = trim($_POST['shopid']);
		
		$query = "INSERT INTO images(imageurl, thumb_imageurl, shop_id)"."VALUES('$uploadedimage','$uploadedThumbImage','$shopid')";
		
		$result = mysql_query($query);
		
		$rowid = mysql_insert_id();
		
		if ($rowid > 0) {
			echo "图片上传成功！<hr><p>";
			echo "<br><a href='viewImages.php?shopid=".$shopid."'>返回图片列表</a>";
		}
		else {
			
			unlink($uploadedimage);
			unlink($uploadedThumbImage);
			
			echo "图片上传失败！<hr><p>";
			echo "<br><a href='viewImages.php?shopid=".$shopid."'>重新添加图片</a>";
			
		}
		
	 }
	else
		{
			echo "<script language=javascript>alert('上传失败'); history.go(-1)</script>";
		}
	}
?>